package com.hippo.gateway.filter;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @ClassName GatewayLogFilter
 * @Description TODO 拦截日志类
 * @Author tangxl
 * @Date 2022/7/14 9:38
 **/
@Component
@Slf4j
public class LogGlobalFilter implements GlobalFilter, Ordered {
    //问题:无法注入Bean,需要手动将过滤器加载到容器中
//    @Autowired
//    private SystemFeignService systemFeignService;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("LogGlobalFilter日志记录："+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
//        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
//        if (uname == null) {
//            log.info("************用户名为空！");
//            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
//            return exchange.getResponse().setComplete();
//        }

        String reqPath = exchange.getRequest().getPath().toString();
        String reqParams = exchange.getRequest().getQueryParams().toString();
        String reqBodys = exchange.getRequest().getBody().toString();
        log.info("请求路径："+reqPath);
        log.info("params参数："+reqParams);
        log.info("body参数："+reqBodys);
//        systemFeignService.saveInterfaceLog("接口路径"+reqPath);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
